#include <QTabWidget>
#include <QVBoxLayout>

#include <pluginui/manager/spi/TabPanelContainerFactory.h>

namespace antu
{
namespace plugin
{
namespace ui
{
TabPanelContainer::TabPanelContainer(const QString& name, const QString& title,
                  Qt::DockWidgetArea area,
                  Qt::DockWidgetAreas allowedAreas,
                  QDockWidget::DockWidgetFeatures features)
    : AbstractPanelContainer(name, area, allowedAreas, features)
{
    this->m_pWidget = new QTabWidget;
    this->m_pWidget->setContentsMargins(0, 0, 0, 0);
    this->m_pWidget->setWindowTitle(title);
    this->m_pWidget->setObjectName(name);
}

void TabPanelContainer::addSeparator(QFrame::Shadow /*shadow*/)
{
    return;
}

QWidget* TabPanelContainer::containerWidget()
{
    return this->m_pWidget;
}

void TabPanelContainer::currentTabChanged(int index)
{
    QTabWidget* pTab = (QTabWidget*)this->m_pWidget;
    QWidget* p = pTab->widget(index);

    if (p != nullptr && this->changeWindowTitleWithTab())
    {
        pTab->setWindowTitle(p->windowTitle());
        emit tabChanged(pTab);
    }
}

void TabPanelContainer::addToContainer(PanelItem* /*pPanelItem*/, int /*stretch*/)
{

}

void TabPanelContainer::doFinalize()
{
    if (this->m_oPanelItems.size() == 1)
    {
        QWidget* pOld = this->m_pWidget;
        this->m_pWidget = new QWidget;
        this->m_pWidget->setWindowTitle(pOld->windowTitle());
        this->m_pWidget->setObjectName(pOld->objectName());

        QVBoxLayout* pLayout = new QVBoxLayout;
        this->m_pWidget->setLayout(pLayout);

        pLayout->addWidget(this->m_oPanelItems[0]->widget());
        delete pOld;
    }
    else
    {
        auto iter = this->m_oPanelItems.begin();
        auto end = this->m_oPanelItems.end();

        QTabWidget* pTab = (QTabWidget*)this->m_pWidget;
        for (; iter != end; iter++)
        {
            QWidget* pWidget = (*iter)->widget();

            if (pWidget->windowIcon().isNull())
            {
                ((QTabWidget*)this->m_pWidget)->addTab(pWidget, pWidget->windowTitle());
            }
            else
            {
                ((QTabWidget*)this->m_pWidget)->addTab(pWidget, pWidget->windowIcon(), pWidget->windowTitle());
            }
        }

        if (this->changeWindowTitleWithTab())
        {
            connect(pTab, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));
            //pTab->setWindowTitle((*iter)->widget()->windowTitle());

            if (this->m_oPanelItems.size() > 0)
            {
                int index = pTab->currentIndex();

				if (index < 0)
					index = 0;
                pTab->setWindowTitle(this->m_oPanelItems.at(index)->widget()->windowTitle());
            }
        }
    }
}

void TabPanelContainer::setChangeWindowTitleWithTab(bool changeWithTab)
{
    this->m_bChangeWindowTitleWithTab = changeWithTab;

    if (this->m_bChangeWindowTitleWithTab && this->m_oPanelItems.size() > 0)
    {
        int index = ((QTabWidget*)this->m_pWidget)->currentIndex();

		if (index < 0)
			index = 0;
        this->m_pWidget->setWindowTitle(this->m_oPanelItems.at(index)->widget()->windowTitle());
    }
}

TabPanelContainerFactory::TabPanelContainerFactory()
{
}

AbstractPanelContainer *TabPanelContainerFactory::createContainer(const QString &name, const QString &title, Qt::DockWidgetArea area, Qt::DockWidgetAreas allowedAreas, QDockWidget::DockWidgetFeatures features)
{
    TabPanelContainer* pContainer = new TabPanelContainer(name, title, area, allowedAreas, features);
    return pContainer;
}
}
}
}

